package com.foxmail.aroundme.boring.manager;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityManager;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.foxmail.aroundme.boring.BuildConfig;
import com.foxmail.aroundme.boring.app.App;
import com.socks.library.KLog;

import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by lianzhan on 16/3/24.
 */
public class UIHelper {
    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
    //新闻详情padding
    private static final int NEWS_DETAIL_PADDING = 15 * 2;


    private static void initWebView(WebView webView) {
        disableAccessibility(App.getContext());
        WebSettings settings = webView.getSettings();
        webView.setBackgroundColor(0); // 设置背景色
        webView.setHorizontalScrollBarEnabled(true);
        webView.setVerticalScrollBarEnabled(true);
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setSupportMultipleWindows(false);
        settings.setDisplayZoomControls(false);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setDefaultTextEncodingName("UTF-8");
    }

    public static void initWebViewDetail(WebView webView) {
        WebSettings settings = webView.getSettings();
        initWebView(webView);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
    }

    public static void initWebViewSource(WebView webView) {
        WebSettings settings = webView.getSettings();
        initWebView(webView);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
    }

    public static void initWebViewOpera(WebView webView) {
        WebSettings settings = webView.getSettings();
        initWebView(webView);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(true);
    }

    //判断email格式是否正确
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 关闭辅助功能，针对4.2.1和4.2.2 崩溃问题
     * java.lang.NullPointerException
     * at android.webkit.AccessibilityInjector$TextToSpeechWrapper$1.onInit(AccessibilityInjector.java:753)
     * at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:321)
     */
    private static void disableAccessibility(Context context) {
        if (Build.VERSION.SDK_INT != Build.VERSION_CODES.JELLY_BEAN_MR1 || context == null) {
            return;
        }
        try {
            AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
            if (!am.isEnabled()) {
                //Not need to disable accessibility
                return;
            }
            Method setState = am.getClass().getDeclaredMethod("setState", int.class);
            setState.setAccessible(true);
            setState.invoke(am, 0);/**{@link AccessibilityManager#STATE_FLAG_ACCESSIBILITY_ENABLED}*/
        } catch (Exception ignored) {

        } catch (Error ignored) {

        }
    }

    /**
     * 生成viewId
     *
     * @return
     */
    public static int generateViewId() {
        for (; ; ) {
            final int result = sNextGeneratedId.get();
            // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
            if (sNextGeneratedId.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }

}
